package com.myphoto.business.photo.controller;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.myphoto.base.annotation.ProductSwagger;
import com.myphoto.base.annotation.SysLog;
import com.myphoto.base.config.properties.IConfig;
import com.myphoto.base.enumtype.LogTypeEnum;
import com.myphoto.base.enumtype.ModuleEnum;
import com.myphoto.base.enumtype.MsgEnum;
import com.myphoto.base.util.R;
import com.myphoto.business.photo.model.PhoneListModel;
import com.myphoto.business.photo.service.PhoneService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/phone")
@Api(tags = "通讯录管理")
public class PhoneListController {

    @Autowired
    private PhoneService phoneService;
    @Autowired
    private IConfig config;

    /**
    * @description 查询通讯录列表
    * @author 方安伦
    * @date 2022/09/08 09:22
     **/
    @ApiOperation(value = "查询已上传的通讯录列表", notes = "查询已上传的通讯录列表",httpMethod="POST")
    @ApiOperationSupport( author = "方安伦")
    @RequestMapping("/loadPhoneList")
    @ProductSwagger
    @SysLog(model = ModuleEnum.PHONE,logType = LogTypeEnum.INFO,value = "查询已上传的通讯录列表",saveToDatabase = true)
    public R<PhoneListModel> loadPhoneList() throws Exception{
        PhoneListModel phoneListModel = phoneService.loadPhoneList();
        return R.success(MsgEnum.OPRATE_SUCCESS,phoneListModel);
    }

    /**
     * @description 上传通讯录
     * @author 方安伦
     * @date 2022/09/08 09:29
     **/
    @ApiOperation(value = "上传通讯录", notes = "上传通讯录",httpMethod="POST")
    @ApiOperationSupport( author = "方安伦")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "phoneListJSON", value = "通讯录JSON格式数据，该JSON字符串是实体【PhoneListModel】", dataType = "string", paramType = "query", dataTypeClass=String.class ,required = true)
    })
    @RequestMapping("/uploadPhoneList")
    @SysLog(model = ModuleEnum.PHOTO,logType = LogTypeEnum.INFO,value = "上传通讯录")
    public R uploadPhoneList(String phoneListJSON) throws Exception{
        PhoneListModel phoneList = JSONUtil.toBean(phoneListJSON, PhoneListModel.class);
        //1、检查上传的数据是否为空
        if(phoneList==null || phoneList.getPhoneModelList()==null || phoneList.getPhoneModelList().size()==0){
            return R.error("上传的通讯录为空！");
        }
        //2、上传通讯录
        phoneService.uploadPhoneList(phoneList);
        return R.success(MsgEnum.UPLOAD_SUCCESS);
    }

}
